package com.personnel_management.dao;

import com.personnel_management.pojo.Announcement;
import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface AnnounDao {
    //发布公告

    @Insert("INSERT INTO announcement (title,content,starttime) VALUES (#{title},#{content},#{starttime})")
    public Integer addAnnoun(Announcement a);
    //删除公告(根据id)
    //DELETE FROM announcement WHERE id=3;
    @Delete("DELETE FROM announcement WHERE id=#{id}")
    public Integer deleteAnnoun(Integer id);
    //修改公告
    //UPDATE announcement SET title='111' AND content='222' WHERE id=1;
    //特别注意不能使用AND,要使用,
    @UpdateProvider(type=SetAnnoun.class,method ="setAnnounByid" )
    public Integer setAnnounByid(Announcement a);
    //修改公告SQL语句的内部类
    class SetAnnoun{
        public String setAnnounByid(Announcement a)
        {
            StringBuilder builder=new StringBuilder("UPDATE announcement SET ");
            //至少修改公告的标题、内容的任意一项
            if(a.getTitle()!=null&&a.getContent()==null)
            {
                //设置标题
                builder.append("title=#{title}");
            }
            else if(a.getTitle()==null&&a.getContent()!=null)
            {
                //设置内容
                builder.append("content=#{content}");
            }
            else if(a.getTitle()!=null&&a.getContent()!=null)
            {
                //修改标题和内容
                builder.append("title=#{title} , content=#{content}");
            }
            builder.append(" WHERE id=#{id}");
            return builder.toString();

        }
    }
    //查询公告(全部)
    @Select({"SELECT * FROM announcement"})
    public List<Announcement> getAnnouns();
}
